home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / libwww2 / HTML.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  13.0 KB  |  527 lines

  1. /*        Structured stream to Rich hypertext converter
  2. **        ============================================
  3. **
  4. **    This generates a hypertext object.  It converts from the
  5. **    structured stream interface from HTML events into the style-
  6. **    oriented interface of the HText.h interface.  This module is
  7. **    only used in clients and should not be linked into servers.
  8. **
  9. **    Override this module if you are making a new GUI browser.
  10. **
  11. */
  12. #include "HTML.h"
  13.  
  14. #include <ctype.h>
  15. #include <stdio.h>
  16.  
  17. #include "HTAtom.h"
  18. #include "HTChunk.h"
  19. #include "HText.h"
  20.  
  21. #include "HTAlert.h"
  22. #include "HTMLGen.h"
  23. #include "HTParse.h"
  24.  
  25. /* #define TRACE 1 */
  26.  
  27. /*        HTML Object
  28. **        -----------
  29. */
  30. #define MAX_NESTING 20        /* Should be checked by parser */
  31.  
  32. struct _HTStructured {
  33.     CONST HTStructuredClass *     isa;
  34.     HTParentAnchor *         node_anchor;
  35.     HText *             text;
  36.  
  37.     HTStream*            target;            /* Output stream */
  38.     HTStreamClass        targetClass;        /* Output routines */
  39. };
  40.  
  41. struct _HTStream {
  42.     CONST HTStreamClass *    isa;
  43.     /* .... */
  44. };
  45.  
  46. /*     Entity values -- for ISO Latin 1 local representation
  47. **
  48. **    This MUST match exactly the table referred to in the DTD!
  49. */
  50. static char * ISO_Latin1[] = {
  51.       "\306",    /* capital AE diphthong (ligature) */ 
  52.       "\301",    /* capital A, acute accent */ 
  53.       "\302",    /* capital A, circumflex accent */ 
  54.       "\300",    /* capital A, grave accent */ 
  55.       "\305",    /* capital A, ring */ 
  56.       "\303",    /* capital A, tilde */ 
  57.       "\304",    /* capital A, dieresis or umlaut mark */ 
  58.       "\307",    /* capital C, cedilla */ 
  59.       "\320",    /* capital Eth, Icelandic */ 
  60.       "\311",    /* capital E, acute accent */ 
  61.       "\312",    /* capital E, circumflex accent */ 
  62.       "\310",    /* capital E, grave accent */ 
  63.       "\313",    /* capital E, dieresis or umlaut mark */ 
  64.       "\315",    /* capital I, acute accent */ 
  65.       "\316",    /* capital I, circumflex accent */ 
  66.       "\314",    /* capital I, grave accent */ 
  67.       "\317",    /* capital I, dieresis or umlaut mark */ 
  68.       "\321",    /* capital N, tilde */ 
  69.       "\323",    /* capital O, acute accent */ 
  70.       "\324",    /* capital O, circumflex accent */ 
  71.       "\322",    /* capital O, grave accent */ 
  72.       "\330",    /* capital O, slash */ 
  73.       "\325",    /* capital O, tilde */ 
  74.       "\326",    /* capital O, dieresis or umlaut mark */ 
  75.       "\336",    /* capital THORN, Icelandic */ 
  76.       "\332",    /* capital U, acute accent */ 
  77.       "\333",    /* capital U, circumflex accent */ 
  78.       "\331",    /* capital U, grave accent */ 
  79.       "\334",    /* capital U, dieresis or umlaut mark */ 
  80.       "\335",    /* capital Y, acute accent */ 
  81.       "\341",    /* small a, acute accent */ 
  82.       "\342",    /* small a, circumflex accent */ 
  83.       "\346",    /* small ae diphthong (ligature) */ 
  84.       "\340",    /* small a, grave accent */ 
  85.       "\046",    /* ampersand */ 
  86.       "\345",    /* small a, ring */ 
  87.       "\343",    /* small a, tilde */ 
  88.       "\344",    /* small a, dieresis or umlaut mark */ 
  89.       "\347",    /* small c, cedilla */ 
  90.       "\351",    /* small e, acute accent */ 
  91.       "\352",    /* small e, circumflex accent */ 
  92.       "\350",    /* small e, grave accent */ 
  93.       "\360",    /* small eth, Icelandic */ 
  94.       "\353",    /* small e, dieresis or umlaut mark */ 
  95.       "\076",    /* greater than */ 
  96.       "\355",    /* small i, acute accent */ 
  97.       "\356",    /* small i, circumflex accent */ 
  98.       "\354",    /* small i, grave accent */ 
  99.       "\357",    /* small i, dieresis or umlaut mark */ 
  100.       "\074",    /* less than */ 
  101.       "\361",    /* small n, tilde */ 
  102.       "\363",    /* small o, acute accent */ 
  103.       "\364",    /* small o, circumflex accent */ 
  104.       "\362",    /* small o, grave accent */ 
  105.       "\370",    /* small o, slash */ 
  106.       "\365",    /* small o, tilde */ 
  107.       "\366",    /* small o, dieresis or umlaut mark */ 
  108.       "\337",    /* small sharp s, German (sz ligature) */ 
  109.       "\376",    /* small thorn, Icelandic */ 
  110.       "\372",    /* small u, acute accent */ 
  111.       "\373",    /* small u, circumflex accent */ 
  112.       "\371",    /* small u, grave accent */ 
  113.       "\374",    /* small u, dieresis or umlaut mark */ 
  114.       "\375",    /* small y, acute accent */ 
  115.       "\377",    /* small y, dieresis or umlaut mark */ 
  116. };
  117.  
  118.  
  119.  
  120. /*        Set character set
  121. **        ----------------
  122. */
  123.  
  124. PRIVATE char** p_entity_values = ISO_Latin1;    /* Pointer to translation */
  125.  
  126. PUBLIC void HTMLUseCharacterSet ARGS1(HTMLCharacterSet, i)
  127. {
  128.     p_entity_values = ISO_Latin1;
  129. }
  130.  
  131.  
  132. /*_________________________________________________________________________
  133. **
  134. **            A C T I O N     R O U T I N E S
  135. */
  136.  
  137. /*    Character handling
  138. **    ------------------
  139. */
  140. PRIVATE void HTML_put_character ARGS2(HTStructured *, me, char, c)
  141. {
  142.   if (!me->text) 
  143.     {
  144.       me->text = HText_new();
  145.       HText_beginAppend(me->text);
  146.     }
  147.   HText_appendCharacter(me->text, c);
  148. }
  149.  
  150.  
  151.  
  152. /*    String handling
  153. **    ---------------
  154. **
  155. **    This is written separately from put_character becuase the loop can
  156. **    in some cases be promoted to a higher function call level for speed.
  157. */
  158. PRIVATE void HTML_put_string ARGS2(HTStructured *, me, CONST char*, s)
  159. {
  160.   if (!me->text) 
  161.     {
  162.       me->text = HText_new();
  163.       HText_beginAppend(me->text);
  164.     }
  165.   HText_appendText(me->text, s);
  166. }
  167.  
  168.  
  169. /*    Buffer write
  170. **    ------------
  171. */
  172. PRIVATE void HTML_write ARGS3(HTStructured *, me, CONST char*, s, int, l)
  173. {
  174.     CONST char* p;
  175.     CONST char* e = s+l;
  176.     for (p=s; s<e; p++) HTML_put_character(me, *p);
  177. }
  178.  
  179.  
  180. /*    Start Element
  181. **    -------------
  182. */
  183. PRIVATE void HTML_start_element ARGS4(
  184.     HTStructured *,     me,
  185.     int,        element_number,
  186.     CONST BOOL*,         present,
  187.     CONST char **,    value)
  188. {
  189.   if (!me->text) 
  190.     {
  191.       me->text = HText_new();
  192.       HText_beginAppend(me->text);
  193.     }
  194.   switch (element_number) 
  195.     {
  196.     case HTML_A:
  197.       {
  198.         char *href = NULL;
  199.         if (present[HTML_A_HREF]) 
  200.           {
  201.             StrAllocCopy(href, value[HTML_A_HREF]);
  202.             HTSimplify(href);
  203.           }
  204.         HText_beginAnchor(me->text, href);
  205.         free (href);
  206.       }
  207.       break;
  208.       
  209.     case HTML_TITLE:
  210.       HText_appendText(me->text, "<TITLE>");
  211.       break;
  212.     case HTML_ISINDEX:
  213.       HText_appendText(me->text, "<ISINDEX>\n");
  214.       break;
  215.     case HTML_P:
  216.       HText_appendText(me->text, "<P>\n");
  217.       break;
  218.     case HTML_DL:
  219.       HText_appendText(me->text, "\n<DL>\n");
  220.       break;
  221.     case HTML_DT:
  222.       HText_appendText(me->text, "\n<DT> ");
  223.       break;
  224.     case HTML_DD:
  225.       HText_appendText(me->text, "\n<DD> ");
  226.       break;
  227.     case HTML_UL:
  228.       HText_appendText(me->text, "\n<UL>\n");
  229.       break;
  230.     case HTML_OL:
  231.       HText_appendText(me->text, "\n<OL>\n");
  232.       break;
  233.     case HTML_MENU:
  234.       HText_appendText(me->text, "\n<MENU>\n");
  235.       break;
  236.     case HTML_DIR:
  237.       HText_appendText(me->text, "\n<DIR>\n");
  238.       break;
  239.     case HTML_LI:
  240.       HText_appendText(me->text, "\n<LI> ");
  241.       break;
  242.     case HTML_LISTING:
  243.       HText_appendText(me->text, "<LISTING>");
  244.       break;
  245.     case HTML_XMP:
  246.       HText_appendText(me->text, "<XMP>");
  247.       break;
  248.     case HTML_PLAINTEXT:
  249.       HText_appendText(me->text, "<PLAINTEXT>");
  250.       break;
  251.     case HTML_PRE:
  252.       HText_appendText(me->text, "<PRE>");
  253.       break;
  254.     case HTML_IMG:
  255.       {
  256.         char *href = NULL;
  257.         if (present[HTML_A_HREF]) 
  258.           {
  259.             StrAllocCopy(href, value[HTML_A_HREF]);
  260.             HTSimplify(href);
  261.           }
  262.         if (href)
  263.           {
  264.             HText_appendText(me->text, "<IMG SRC=\"");
  265.             HText_appendText(me->text, href);
  266.             HText_appendText(me->text, "\">");
  267.             free (href);
  268.           }
  269.       }
  270.       break;
  271.     case HTML_H1:
  272.       HText_appendText(me->text, "<H1>");
  273.       break;
  274.     case HTML_H2:
  275.       HText_appendText(me->text, "<H2>");
  276.       break;
  277.     case HTML_H3:
  278.       HText_appendText(me->text, "<H3>");
  279.       break;
  280.     case HTML_H4:
  281.       HText_appendText(me->text, "<H4>");
  282.       break;
  283.     case HTML_H5:
  284.       HText_appendText(me->text, "<H5>");
  285.       break;
  286.     case HTML_H6:
  287.       HText_appendText(me->text, "<H6>");
  288.       break;
  289.     case HTML_ADDRESS:
  290.       HText_appendText(me->text, "<ADDRESS>");
  291.       break;
  292.     case HTML_BLOCKQUOTE:
  293.       HText_appendText(me->text, "<BLOCKQUOTE>");
  294.       break;
  295.     
  296.     case HTML_TT:            /* Physical character highlighting */
  297.     case HTML_B:            /* Currently ignored */
  298.     case HTML_I:
  299.     case HTML_U:
  300.     case HTML_EM:            /* Logical character highlighting */
  301.     case HTML_STRONG:            /* Currently ignored */
  302.     case HTML_CODE:
  303.     case HTML_SAMP:
  304.     case HTML_KBD:
  305.     case HTML_VAR:
  306.     case HTML_DFN:
  307.     case HTML_CITE:
  308.       break;
  309.  
  310.     default:
  311.       break;
  312.       
  313.     } /* end switch */
  314. }
  315.  
  316.  
  317. /*        End Element
  318. **        -----------
  319. **
  320. */
  321. PRIVATE void HTML_end_element ARGS2(HTStructured *, me, int , element_number)
  322. {
  323.   switch(element_number) 
  324.     {
  325.     case HTML_A:
  326.       HText_endAnchor(me->text);
  327.       break;
  328.     case HTML_TITLE:
  329.       HText_appendText(me->text, "</TITLE>\n");
  330.       break;
  331.     case HTML_LISTING:
  332.       HText_appendText(me->text, "</LISTING>\n");
  333.       break;
  334.     case HTML_XMP:
  335.       HText_appendText(me->text, "</XMP>\n");
  336.       break;
  337.     case HTML_PRE:
  338.       HText_appendText(me->text, "</PRE>\n");
  339.       break;
  340.     case HTML_DL:
  341.       HText_appendText(me->text, "\n</DL>\n");
  342.       break;
  343.     case HTML_UL:
  344.       HText_appendText(me->text, "\n</UL>\n");
  345.       break;
  346.     case HTML_OL:
  347.       HText_appendText(me->text, "\n</OL>\n");
  348.       break;
  349.     case HTML_MENU:
  350.       HText_appendText(me->text, "\n</MENU>\n");
  351.       break;
  352.     case HTML_DIR:
  353.       HText_appendText(me->text, "\n</DIR>\n");
  354.       break;
  355.     case HTML_H1:
  356.       HText_appendText(me->text, "</H1>\n");
  357.       break;
  358.     case HTML_H2:
  359.       HText_appendText(me->text, "</H2>\n");
  360.       break;
  361.     case HTML_H3:
  362.       HText_appendText(me->text, "</H3>\n");
  363.       break;
  364.     case HTML_H4:
  365.       HText_appendText(me->text, "</H4>\n");
  366.       break;
  367.     case HTML_H5:
  368.       HText_appendText(me->text, "</H5>\n");
  369.       break;
  370.     case HTML_H6:
  371.       HText_appendText(me->text, "</H6>\n");
  372.       break;
  373.     case HTML_ADDRESS:
  374.       HText_appendText(me->text, "</ADDRESS>\n");
  375.       break;
  376.     case HTML_BLOCKQUOTE:
  377.       HText_appendText(me->text, "</BLOCKQUOTE>\n");
  378.       break;
  379.     default:
  380.       break;
  381.     } /* switch */
  382. }
  383.  
  384.  
  385. /*        Expanding entities
  386. **        ------------------
  387. */
  388. /*    (In fact, they all shrink!)
  389. */
  390.  
  391. PRIVATE void HTML_put_entity ARGS2(HTStructured *, me, int, entity_number)
  392. {
  393.     HTML_put_string(me, ISO_Latin1[entity_number]);    /* @@ Other representations */
  394. }
  395.  
  396.  
  397.  
  398. /*    Free an HTML object
  399. **    -------------------
  400. **
  401. ** If the document is empty, the text object will not yet exist.
  402.    So we could in fact abandon creating the document and return
  403.    an error code.  In fact an empty document is an important type
  404.    of document, so we don't.
  405. **
  406. **    If non-interactive, everything is freed off.   No: crashes -listrefs
  407. **    Otherwise, the interactive object is left.    
  408. */
  409. PUBLIC void HTML_free ARGS1(HTStructured *, me)
  410. {
  411.   if (me->text)
  412.     HText_endAppend(me->text);
  413.   
  414.   if (me->target) 
  415.     {
  416.       (*me->targetClass.end_document)(me->target);
  417.       (*me->targetClass.free)(me->target);
  418.     }
  419.   free(me);
  420. }
  421.  
  422.  
  423. PUBLIC void HTML_handle_interrupt ARGS1(HTStructured *, me)
  424. {
  425.   if (me->text)
  426.     HText_doAbort (me->text);
  427.   
  428.   if (me->target) 
  429.     {
  430.       (*me->targetClass.handle_interrupt)(me->target);
  431.     }
  432.   /* Not necessarily safe... */
  433.   /* free(me); */
  434. }
  435.  
  436.  
  437. PRIVATE void HTML_end_document ARGS1(HTStructured *, me)
  438. {            /* Obsolete */
  439. }
  440.  
  441.  
  442. /*    Structured Object Class
  443. **    -----------------------
  444. */
  445. PUBLIC CONST HTStructuredClass HTMLPresentation = /* As opposed to print etc */
  446. {        
  447.     "text/html",
  448.     HTML_free,
  449.     HTML_end_document, HTML_handle_interrupt,
  450.     HTML_put_character,     HTML_put_string,  HTML_write,
  451.     HTML_start_element,     HTML_end_element,
  452.     HTML_put_entity
  453. }; 
  454.  
  455.  
  456. /*        New Structured Text object
  457. **        --------------------------
  458. **
  459. **    The strutcured stream can generate either presentation,
  460. **    or plain text, or HTML.
  461. */
  462. PUBLIC HTStructured* HTML_new ARGS3(
  463.     HTParentAnchor *,     anchor,
  464.     HTFormat,        format_out,
  465.     HTStream*,        stream)
  466. {
  467.  
  468.     HTStructured * me;
  469.     
  470. #if 0
  471.     if (format_out != WWW_PLAINTEXT && format_out != WWW_PRESENT) {
  472.         HTStream * intermediate = HTStreamStack(WWW_HTML, format_out, 0,
  473.         stream, anchor);
  474.         fprintf (stderr, "+++ YO in HTML_new\n");
  475.     if (intermediate) return HTMLGenerator(intermediate);
  476.         fprintf(stderr, "** Internal error: can't parse HTML to %s\n",
  477.                HTAtom_name(format_out));
  478.     exit (-99);
  479.     }
  480. #endif
  481.  
  482.     me = (HTStructured*) malloc(sizeof(*me));
  483.     if (me == NULL) outofmem(__FILE__, "HTML_new");
  484.  
  485.     me->isa = &HTMLPresentation;
  486.     me->node_anchor =  anchor;
  487.     me->text = 0;
  488.     me->target = stream;
  489.     if (stream) 
  490.       me->targetClass = *stream->isa;    /* Copy pointers */
  491.     
  492.     return (HTStructured*) me;
  493. }
  494.  
  495.  
  496. /*    Record error message as a hypertext object
  497. **    ------------------------------------------
  498. **
  499. **    The error message should be marked as an error so that
  500. **    it can be reloaded later.
  501. **    This implementation just throws up an error message
  502. **    and leaves the document unloaded.
  503. **    A smarter implementation would load an error document,
  504. **    marking at such so that it is retried on reload.
  505. **
  506. ** On entry,
  507. **    sink     is a stream to the output device if any
  508. **    number    is the HTTP error number
  509. **    message    is the human readable message.
  510. **
  511. ** On exit,
  512. **    returns    a negative number to indicate lack of success in the load.
  513. */
  514.  
  515. PUBLIC int HTLoadError ARGS3(
  516.     HTStream *,     sink,
  517.     int,        number,
  518.     CONST char *,    message)
  519. {
  520.     HTAlert(message);        /* @@@@@@@@@@@@@@@@@@@ */
  521. #if 0
  522.     return -number;
  523. #endif
  524.     return -1;
  525.  
  526.